$(function(){
	 $.ajaxSetup({cache:false});//ajax调用不使用缓存
	//JQeury插件主页面框体显示
	$("#functionMethodList").jqGrid({
	   	url:'functionMethod.do?type=list',
	   	height:450,   	
		datatype: "xml",
	   	colNames:['功能编码','功能名称','功能方法'],			
	   	colModel:[
	   	          	{name:'functionMethodId', width:100,xmlmap:'functionMethodId'},
	   	          	{name:'functionMethodName', width:100,xmlmap:'functionMethodName'},
	   	          	{name:'methods', width:500,xmlmap:'methods'}
	   	],
	   	xmlReader:{
	   		repeatitems:false
	   	},
	   	pager: $('#functionMethodPager'),
	   	pgbuttons: false,
		pginput: false,
	   	rowNum:100,
	   	autowidth: true,
	   	sortname: 'id',
	    viewrecords: true,
	    gridComplete:function(){
    		resizeJQGrid({jqGrid:this.id,userData:false});
    	},
    	caption:"系统方法列表"
	}).navGrid('#functionMethodPager',{edit:false,add:false,del:false,refresh:false,search:false});//.navButtonAdd("#functionMethodPager",{
//			caption : "编辑",
//			title: '编辑所选择记录明细',
//			buttonicon : 'ui-icon-pencil',
//			onClickButton:processEdit,
//			position : "first",
//			cursor : 'pointer'
//	});
	
	if(p_update){
		$("#functionMethodList").navButtonAdd("#functionMethodPager",{
			caption : "编辑",
			title: '编辑所选择记录明细',
			buttonicon : 'ui-icon-pencil',
			onClickButton:processEdit,
			position : "first",
			cursor : 'pointer'
	});
	}
	
	//编辑窗口的初始化
	$("#detailDialog").dialog({
		bgiframe: true,
		autoOpen:false,
		width:500,
		stack:false,
		modal:true,
		position:'center',			
		title:'详细信息',
		buttons:{"取消":function(){$("#detailDialog").dialog("close");},"保存":updateMethod},
		open:function(){
			$("#detailDialog").parent().find(".ui-dialog-buttonpane button:visible").focus();
		}	
	});
	
	//功能编辑文本框单击或获取焦点后使用的窗口初始化
	$("#chooseMethodDialog").dialog({
		bgiframe: true,
		autoOpen:false,
		width:300,
		//height:500,
		stack:false,
		modal:true,
		position:'center',			
		title:'详细信息',
		buttons:{"取消":function(){$("#chooseMethodDialog").dialog("close");},"确定":chooseMethodSave},
		open:function(){
			$("#detailDialog").parent().find(".ui-dialog-buttonpane button:visible").focus();
		}	
	});
	
	//为功能编辑的text增加单击事件和获取焦点事件
	$("#method").click(function(){
		methodChoose();
	});
	
	$("#method").focus(function(){
		methodChoose();
	});
	
	//异步加载所有的方法列表放到chooseMethod div块下
	$.post(
		"functionMethod.do?type=methodLoad",
		function(html){
			$("#chooseMethod").html(html);
		},"html"
	);
});

function methodChoose(){
	//选择一行作为焦点并且获取当前行的id
	var selectRow = $("#functionMethodList").getGridParam("selrow");
	//窗口弹出前先清空已勾选过的选项
	$("#chooseMethod input").each(function(){
		$(this).removeAttr("checked");
	});
	//功能编辑text文本框在获取焦点或者点击事件后调用函数、该函数用来让用户选择功能对应的那些方法
	$("#chooseMethodDialog").dialog("open");
	
	//从页面上获得的已选择的选项。可以更准确的获得数据
	var s = $("#methodId").val();
	if($.trim(s) != "" && $.trim(s).length > 0){
		var s1 = s.split(",");
		for(var i=0;i<s1.length;i=i+1){
			$("#" + s1[i]).attr("checked","checked");
		}
	}
}

//方法选择后确定操作
function chooseMethodSave(){
	var str = "";
	var id = "";
	$("#chooseMethod input:checked").each(function(){
		str = str + $(this).val() + ",";
		id = id + $(this).attr("id") + ",";
	});
	if(str != ""){
		str = str.substr(0,str.length - 1);
		$("#method").val(str);
	}else{
		$("#method").val(str);
	}
	
	if(id != ""){
		id = id.substr(0,id.length - 1);
		$("#methodId").val(id);
	}else{
		$("#methodId").val(id);
	}
	//选择完成后窗口关闭
	$("#chooseMethodDialog").dialog("close");
}

//编辑窗口的显示
function processEdit(){
	//选择前先清空文本框中以前缓存的数据
	$("#method").val("");
	$("#methodId").val("");
	//选择一行作为焦点并且获取当前行的id
	var selectRow = $("#functionMethodList").getGridParam("selrow");

	if(!selectRow){
	 	//警告对话框的初始化
 		var alertIDs = {themodal:'alertmod',modalhead:'alerthd',modalcontent:'alertcnt'};
 		viewModal("#"+alertIDs.themodal,{gbox:"#gbox_"+"priceItemList",jqm:true}); 
 		$("#jqg_alrt").focus();
 		return;
	}
	
	$.post(
			"functionMethod.do?type=methodIsChoose",
			{
				funId:selectRow
			},function(xml){
				var str = $(xml).find("str").text();
				if(str != "notData"){
					str = str.substr(0,str.length-1);
					$("#methodId").val(str);
					var s = str.split(",");//js中字符串拆分数组
					for(var i=0;i<s.length;i=i+1){
						$("#" + s[i]).attr("checked","checked");
					}
				}
			},"xml"
		);
	
	//窗口弹出
	$("#detailDialog").dialog("open");
	//不让编号和名称进行修改
	$("#ID").attr("readonly","readonly");
	$("#ID").css("background-color","#BBBBBB");
	$("#name").attr("readonly","readonly");
	$("#name").css("background-color","#BBBBBB");
	//获取某一行的数据对象
	var obj = $("#functionMethodList").getRowData(selectRow);
	//从obj对象中拿到该列上对应的字段值
	$("#ID").val(obj.functionMethodId);
	$("#name").val(obj.functionMethodName);
	$("#method").val(obj.methods);
}

//编辑窗口中更新数据
function updateMethod(){
	//表单中拿到数据后向服务器发送请求更新
	$.post(
		"functionMethod.do?type=updateFunMethod",
		{
			id:$("#ID").val(),
			methodId:$("#methodId").val()
		},function(xml){
			var flag = $(xml).find("flag").text();
			var body = $(xml).find("body").text();
			if(flag == "1"){
				var selectRow = $("#functionMethodList").getGridParam("selrow");
				//拿到表单中更新的数据并保存在一个数组中				
				var dataRow = {
					methods:$("#method").val()
				}
				//把数组中的内容更新到指定的表格行中selectRow为行ID，dataRow为行数据
				$("#functionMethodList").setRowData(selectRow,dataRow);
				alert("更新成功");
				$("#detailDialog").dialog("close");
			}else{
				alert(body);
			}
		},"xml"
	);
}